package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.BusiIndustryAssignMapper;
import com.hq.ims.data.entity.BusiIndustryAssign;
import com.hq.ims.data.vo.*;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 产业标记指派表 服务实现类
 * @since 20241025
 */
@Service
public class BaseBusiIndustryAssignService extends BaseService<BusiIndustryAssignMapper, BusiIndustryAssign> {

    /**
     * 分页
     */
    public List<BusiIndustryAssignMapstruct.BusiIndustryAssignVo> getByPage(
            RequestPage page,
            BusiIndustryAssignMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<BusiIndustryAssign> lq = this.lambdaQuery();
        //排序
        //查询条件

        //执行分页
        lq.page(page);

        List<BusiIndustryAssign> dataList = page.getRecords();
        List<BusiIndustryAssignMapstruct.BusiIndustryAssignVo> voList = BusiIndustryAssignMapstruct.INSTANCE.toBusiIndustryAssignVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiIndustryAssign addData(BusiIndustryAssignMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        BusiIndustryAssign entity = new BusiIndustryAssign();
        entity.setIndustryId(vo.getIndustryId());
        entity.setIndustryName(vo.getIndustryName());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setReceiveStatus(vo.getReceiveStatus());
        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiIndustryAssign editData(BusiIndustryAssignMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);

        //账值新的对象
        BusiIndustryAssign entity = this.getById(vo.getBusiIndustryAssignId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setIndustryId(vo.getIndustryId());
        entity.setIndustryName(vo.getIndustryName());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setReceiveStatus(vo.getReceiveStatus());
        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String busiIndustryAssignId) {
        BusiIndustryAssign data = this.getById(busiIndustryAssignId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public BusiIndustryAssignMapstruct.BusiIndustryAssignVo getDetailVo(String busiIndustryAssignId) {
        BusiIndustryAssign entity = this.getById(busiIndustryAssignId);
        BusiIndustryAssignMapstruct.BusiIndustryAssignVo vo = BusiIndustryAssignMapstruct.INSTANCE.toBusiIndustryAssignVo(entity);
        return vo;
    }

    /**
     * @description: 批量修改产业标记
     * @author: yinzhangxiao
     * @date: 2024/11/17 16:36
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDataBatch(List<BusiIndustryAssignMapstruct.SaveVo> assignVoList) {
        if (CollectionUtils.isNotEmpty(assignVoList)) {
            List<BusiIndustryAssign> busiIndustryAssignList = new ArrayList<>();
            this.lambdaUpdate().eq(BusiIndustryAssign::getBusiInfoId, assignVoList.get(0).getBusiInfoId()).remove();
            assignVoList.forEach(assign -> {
                if(StringUtils.isNotEmpty(assign.getIndustryId())){
                    BusiIndustryAssign busiIndustryAssign = new BusiIndustryAssign();
                    BeanUtils.copyProperties(assign, busiIndustryAssign);
                    busiIndustryAssignList.add(busiIndustryAssign);
                }
            });
            if (CollectionUtils.isNotEmpty(busiIndustryAssignList)){
                this.saveBatch(busiIndustryAssignList);
            }
        }
    }
}